package crp.core.workflow.domain.repositories;

import com.qwlabs.lang.C2;
import crp.core.shared.LifecycleStatus;
import crp.core.workflow.domain.models.WorkflowModelEntity;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Parameters;

import javax.enterprise.context.ApplicationScoped;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

@ApplicationScoped
public class WorkflowModelRepository implements PanacheRepositoryBase<WorkflowModelEntity, String> {

    public Optional<WorkflowModelEntity> findByTopic(String topic, LifecycleStatus status) {
        return this.find("topic=:topic and status=:status",
                Parameters.with("topic", topic)
                        .and("status", status)
        ).firstResultOptional();
    }

    public Stream<WorkflowModelEntity> findByIds(Collection<String> ids) {
        if (C2.isEmpty(ids)) {
            return Stream.empty();
        }
        return find("id in (:ids)", Parameters.with("ids", ids)).stream();
    }
}
